<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-10-15
 * Time: 上午10:13
 */
namespace DB;

use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;

class MongoDefault{
  private static $manager;
  private $dbName;
  private $cName;

  protected function __construct(string $cName, string $dbName = null) {
    $this->dbName = $dbName;
    $this->cName = $cName;
  }

  protected static function getInstance(){
    if (self::$manager == null){
      self::$manager = new Manager(\MongoConfig::ADDRESS,[
        "username" => \MongoConfig::USER,
        "password" => \MongoConfig::PWD]
      );
    }
    return self::$manager;
  }

  protected function getNameSpace(){
    return $this->dbName.".".$this->cName;
  }

  public function getMany($filter,$options = []){
    $manager = self::getInstance();
    $query = new Query($filter,$options);
    try{
      $res = $manager->executeQuery($this->getNameSpace(),$query);
      return $res;
    }catch (\Exception $e){
      echo $e->getMessage();
    }
  }
}